package com.zhongkun.datahome.web.core.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.concurrent.Executor;

/**
 * @Author: lifengsheng
 * @Date: 2025/5/9 15:56
 * @Desc
 */
@Slf4j
@EnableAsync
@Configuration
public class AsyncConfig implements AsyncConfigurer {
    @Resource
    private AsyncTaskExecutor threadPoolTaskExecutor;

    @Override
    public Executor getAsyncExecutor() {
        log.info("开始异步执行---------------------------------");
        return threadPoolTaskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        log.info("异步调用，异常捕获---------------------------------");
        return (ex, method, args) -> log.error("caught ex method: {}, args: {}, ex: {}",
                method.getName(), Arrays.toString(args), ex.getMessage());
    }
}
